using System.Collections;
using System.Web.UI;
using Crawler;

namespace Crawler
{
	/// <summary>
	/// Understands the correspondence between the actual URL and the local path
	/// </summary>
	public class Mapper : IMapper
	{
		Queue mapTable = new Queue();
		public event EntryAddedToMapperHandler EntryAddedToMapper;
		
		public Mapper()
		{
		}

		public int Count{ 
			get{return mapTable.Count;}
		}

		public Pair GetNext()
		{
			if(mapTable.Count > 0)
				return mapTable.Dequeue() as Pair;
			return new Pair(Key.NULL_KEY, "");
		}

		public void Add(IKey key, string path)
		{
			mapTable.Enqueue(new Pair(key, path));
			OnEntryAddedToMapper(new EntryAddedToMapperArgs());
		}

		protected virtual void OnEntryAddedToMapper(EntryAddedToMapperArgs args) 
		{
			if (EntryAddedToMapper != null)
				EntryAddedToMapper(this, args);
		}
	}

	/// <summary>
	/// The delegate method provides a framework to handle the events that have been raised by the Mapper
	/// </summary>
	public delegate void EntryAddedToMapperHandler(object sender, EntryAddedToMapperArgs args);
}
